package org.lhg.activiti.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.lhg.activiti.model.MyDeploy;
import org.lhg.activiti.model.PageBean;
import org.lhg.activiti.util.CommonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

@RestController
@RequestMapping("/deploy")
public class DeployController {
    @Autowired
    private RepositoryService repositoryService;

    /**
     * 上传流程部署文件
     * @param deployFile
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/upload",method = RequestMethod.GET)
    public CommonResult<JSONObject> upload(@RequestParam("deployFile") MultipartFile deployFile)throws Exception{
        repositoryService.createDeployment() .name(deployFile.getOriginalFilename()) .addZipInputStream(new ZipInputStream(deployFile.getInputStream())) .deploy();

        JSONObject result=new JSONObject();
        result.put("success", true);
        return CommonResult.success(result);
    }

    /**
     * 流程部署查询
     * @param page
     * @param limit
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/list",method = RequestMethod.GET)
    public  CommonResult<JSONObject> list(@RequestParam(value="page",defaultValue="0")String page, @RequestParam(value="limit",defaultValue="0")String limit)throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(limit));
        // 返回带分页的结果集合
        List<Deployment> deploymentList=repositoryService.createDeploymentQuery() .orderByDeploymenTime().desc().listPage(pageBean.getStart(), pageBean.getPageSize());
        List<MyDeploy> deployList=new ArrayList<MyDeploy>();

        for(Deployment d:deploymentList){
            MyDeploy myDeploy=new MyDeploy();
            myDeploy.setId(d.getId());
            myDeploy.setName(d.getName());
            myDeploy.setDeploymentTime(d.getDeploymentTime());
            deployList.add(myDeploy);
        }
        long total=repositoryService.createDeploymentQuery().count(); // 获取总记录数

        JSONObject result=new JSONObject();
        JSONArray jsonArray= JSONArray.parseArray(JSON.toJSONString(deployList));
        result.put("rows", jsonArray);
        result.put("total", total);
        return CommonResult.success(result);
    }

    /**
     * 删除流程部署
     * @param map
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/delete",method = RequestMethod.POST)
    public CommonResult<JSONObject> delete(Map<String,String> map)throws Exception{
        String ids=map.get("ids");
        String []idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            repositoryService.deleteDeployment(idsStr[i], true);
        }
        JSONObject result=new JSONObject();
        result.put("success", true);
        return CommonResult.success(result);
    }
}
